package main

import (
	"embed"
	"fmt"
	"github.com/wailsapp/wails/v3/pkg/application"
	"github.com/wailsapp/wails/v3/pkg/events"
	"html/template"
	"log"
)

//go:embed frontend/dist
var assets embed.FS

//go:embed build/icon.png
var icon []byte

//go:embed build/list.html
var listHtml string

var appServer = &AppServer{}
var listTpl, _ = template.New("list").Parse(listHtml)

func main() {
	app := application.New(application.Options{
		Icon: icon,
		Name: "goscrcpy",
		Assets: application.AssetOptions{
			FS:      assets,
			Handler: &FileLoader{},
		},
		Bind: []any{
			appServer,
		},
		Windows: application.WindowsOptions{
			WndProcInterceptor:            nil,
			DisableQuitOnLastWindowClosed: false,
			WebviewUserDataPath:           "./data",
			WebviewBrowserPath:            "",
		},
		Mac: application.MacOptions{
			ApplicationShouldTerminateAfterLastWindowClosed: true,
		},
	})
	app.Events.On("selectDevice", func(event *application.WailsEvent) {
		appServer.closeList()
		err := appServer.ConnPhoneHelpWithDeviceName(fmt.Sprintf("%v", event.Data), appServer.Data.IsTop, appServer.Data.IsAwake)
		if err != nil {
			_, _ = appServer.Msg("错误", err.Error())
		}
	})
	app.On(events.Common.ApplicationStarted, func(event *application.Event) {
		mainWindow := createMainWindow(app)
		appServer.init(app, mainWindow)
		createTrayMenu(app).AttachWindow(appServer.mainWindow).WindowOffset(5)
	})

	err := app.Run()
	if err != nil {
		println("Error:", err.Error())
	}
}

func createMainWindow(app *application.App) *application.WebviewWindow {
	mainWindow := app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
		Frameless: true, DevToolsEnabled: true,
		DisableResize: true,
		AlwaysOnTop:   true, Centered: true,
		BackgroundColour: application.RGBA{Red: 27, Green: 38, Blue: 54, Alpha: 1},
	})
	mainWindow.On(events.Common.WindowClosing, func(event *application.WindowEvent) {
		fmt.Println("WindowClosing")
		appServer.onClose()
	})
	screen2, _ := app.CurrentWindow().GetScreen()
	winW := 360 //int(float32(180) * screen2.Scale)
	winH := 120 //int(float32(60) * screen2.Scale)
	winRealW := int(float32(winW) * screen2.Scale)
	winRealH := int(float32(winH) * screen2.Scale)
	mainWindow.SetSize(winW, winH)
	mainWindow.SetAbsolutePosition(screen2.Bounds.X+screen2.WorkArea.Width-winRealW, screen2.WorkArea.Height-winRealH)

	log.Println("screen :", screen2.Name, screen2.ID, screen2.Size, screen2.Bounds, screen2.WorkArea, screen2.Scale)
	var x, y = mainWindow.AbsolutePosition()
	log.Println("wins :", screen2.WorkArea.Width-winRealW, screen2.WorkArea.Height-winRealH, x, y)
	return mainWindow
}

func createTrayMenu(app *application.App) *application.SystemTray {
	systemTray := app.NewSystemTray()
	systemTray.SetTemplateIcon(icon)
	systemTray.SetIcon(icon).SetLabel("goscrcpy")
	var doShow = func() {
		if appServer.mainWindow == nil || appServer.mainWindow.IsVisible() {
			return
		}
		appServer.mainWindow.Show()
	}
	myMenu := app.NewMenu()
	//myMenu.Add("goscrcpy").SetBitmap(icons.WailsLogoBlackTransparent).SetEnabled(false)
	myMenu.Add("连接设备").OnClick(func(ctx *application.Context) {
		appServer.ConnPhoneWithSelect()
	})
	myMenu.Add("显示").OnClick(func(ctx *application.Context) {
		doShow()
	})
	myMenu.AddSeparator()
	myMenu.Add("退出").OnClick(func(ctx *application.Context) {
		if appServer.mainWindow != nil {
			appServer.mainWindow.Close()
		}
		app.Quit()
	})

	systemTray.SetMenu(myMenu)
	return systemTray
}
